home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0133_X-mode Write Mode Example.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-24  |  4KB  |  158 lines

  1.  
  2. { Illustration on how VGA Write Mode 1 works }
  3. { by Andrew Golovin (2:5080/10@Fidonet)      }
  4. { Can be used at your own risk freely w/o    }
  5. { any charge                                 }
  6. {============================================}
  7. { PREFACE:                                   }
  8. { This example illustrate posibility to save }
  9. { Bitmaps in unused VRam. And use VWM1 to    }
  10. { restore it by 4 pixels at one byte         }
  11. { Use arrows to move "bitmap" on screen.     }
  12. { This example _only_ illustrate this mode   }
  13. { Extremly needs optimization! Don't use it  }
  14. { as is. Just an idea.                       }
  15.  
  16. Uses CRT;
  17. var
  18.   OldMode: Byte;
  19.  
  20. procedure SetWriteMode(Wmode: Byte); assembler;
  21. asm
  22.   Mov     DX,3ceh
  23.   Mov     AL,5
  24.   Out     DX,AL
  25.   Inc     DX
  26.   In      AL,DX
  27.   And     AL,11111100b
  28.   Or      AL,WMode
  29.   Out     DX,AL
  30. end;
  31.  
  32. procedure Init320x200_X; assembler;
  33. asm
  34.   Mov AH,0fh; Int 10h; Mov [OldMode],al; Mov AX,13h; Int 10h;
  35.   Mov DX,3c4h; Mov AL,04h; Out DX,AL; Inc DX; In AL,DX; And AL,011110111b;
  36.   Or AL,000000100b; Out DX,AL; Dec DX; Mov AX,0f02h; Out DX,AX;
  37.   Mov AX,0a000h; Mov ES,AX; XOr DI,DI; Mov AX,0202h; Mov CX,8000h;
  38.   ClD; RepNZ StoSW; Mov DX,3d4h; Mov AL,14h; Out DX,AL; Inc DX;
  39.   In AL,DX; And AL,010111111b; Out DX,AL; Dec DX; Mov AL,017h;
  40.   Out DX,AL; Inc DX; In AL,DX; Or AL,01000000b; Out DX,AL; Mov DX,3d4h;
  41.   Mov AX,80; ShR AX,1; Mov AH,AL; Mov AL,13h; Out DX,AX; Ret
  42. end;
  43.  
  44. Procedure PutPixel(x,y: Word; c: Byte);
  45.   begin
  46.     asm
  47.       Mov    DX,3c4h
  48.       Mov    AL,02
  49.       Out    DX,AL
  50.       Mov    AX,Y
  51.       ShL    AX,4
  52.       Mov    DI,AX
  53.       ShL    AX,2
  54.       Add    DI,AX
  55.       Mov    AX,X
  56.       ShR    AX,2
  57.       Add    DI,AX
  58.       Mov    AX,X
  59.       And    AX,3
  60.       Mov    CL,AL
  61.       Mov    AL,1
  62.       ShL    AL,CL
  63.       Inc    DX
  64.       Out    DX,AL
  65.       Mov    AX,0a000h
  66.       Mov    ES,AX
  67.       Mov    AL,C
  68.       StoSB
  69.     end;
  70.   end;
  71.  
  72. procedure MaskBits(BitsToMask: Byte); assembler;
  73.   asm
  74.     Mov     DX,3ceh
  75.     Mov     AL,8
  76.     Mov     AH,BitsToMask
  77.     Out     DX,AX
  78.   end;
  79.  
  80. Procedure MaskPlanes(PlaneToMask: Byte); assembler;
  81. asm
  82.   Mov     DX,3c4h
  83.   Mov     AL,2
  84.   Out     DX,AL
  85.   Inc     DX
  86.   Mov     AL,PlaneToMask
  87.   Out     DX,AL
  88. End;
  89.  
  90. Procedure StoreBack(x,y,w,h: word; toAddr: word);
  91.   var
  92.     curx,cury: Word;
  93.   begin
  94.     SetWriteMode(1);
  95.     MaskPlanes($f);
  96.     MaskBits($ff);
  97.     For CurY:=Y to Y+H do
  98.       Move(Mem[$a000:CurY*80+x],Mem[$a000:toAddr+(CurY-Y)*W],w);
  99.     SetWriteMode(0);
  100.   end;
  101.  
  102. Procedure RestoreBack(x,y,w,h: word; fromAddr: Word);
  103.   var
  104.     cury,curx: Word;
  105.   begin
  106.     SetWriteMode(1);
  107.     MaskPlanes($f);
  108.     MaskBits($ff);
  109.     For CurY:=Y to Y+H do
  110.       Move(Mem[$a000:fromAddr+(CurY-Y)*W],Mem[$a000:CurY*80+x],w);
  111.     SetWriteMode(0);
  112.   end;
  113.  
  114. var
  115.   x,y: Word;
  116.   curx,cury: Word;
  117.   c: Char;
  118. Begin
  119.   Init320x200_x;
  120.   For x:=0 to 319 do
  121.     For y:=0 to 199 do
  122.       PutPixel(x,y,(x +y) mod 16+16);
  123.   StoreBack(0,0,3,12,16000);
  124.   For x:=0 to 11 do
  125.     For y:=0 to 11 do
  126.       PutPixel(x,y,Random(255));
  127.   StoreBack(0,0,3,12,16200);
  128.   CurX:=0;CurY:=0;
  129.   Repeat
  130.     Repeat Until KeyPressed;
  131.     c:=ReadKey;
  132.     If c=#0
  133.        then
  134.          begin
  135.            RestoreBack(CurX,CurY,3,12,16000);
  136.            c:=ReadKey;
  137.            Case c of
  138.              #80: If CurY<187
  139.                      then
  140.                        Inc(CurY);
  141.              #72: If CurY>0
  142.                      Then
  143.                        Dec(CurY);
  144.              #75: If CurX>0
  145.                      Then
  146.                        Dec(CurX);
  147.              #77: If CurX<77
  148.                      Then
  149.                        Inc(CurX);
  150.            end;
  151.            StoreBack(CurX,CurY,3,12,16000);
  152.            RestoreBack(CurX,CurY,3,12,16200);
  153.          end;
  154.   Until c=#27;
  155.   asm Mov al,OldMode; XOr AH,AH; Int 10h end;
  156. End.
  157.  
  158.